//
//  PYShowMediaGestureDismissAnimation.m
//  IHome
//
//   Created by zhujizheng on 2020/2/17.
//  Copyright © 2019年 com.zhujizheng. All rights reserved.
//

#import "PYShowMediaGestureDismissAnimation.h"
#import "PYShowMediaController.h"

@implementation PYShowMediaGestureDismissAnimation

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
    return PY_Animation_Duration;
}

- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    PYShowMediaController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    
    UIView *toView = nil;
    UIView *fromView = nil;
    
    if ([transitionContext respondsToSelector:@selector(viewForKey:)]) {
        fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
        toView = [transitionContext viewForKey:UITransitionContextToViewKey];
    } else {
        fromView = fromVC.view;
        toView = toVC.view;
    }
    
    [[transitionContext containerView] insertSubview:toView belowSubview:fromView];
    
    fromView.backgroundColor = [UIColor blackColor];
    
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        fromView.backgroundColor = [UIColor clearColor];
        fromVC.pageControl.alpha = 0;
    } completion:^(BOOL finished){
        if (finished) {
            BOOL wasCancelled = [transitionContext transitionWasCancelled];
            if (wasCancelled) {
                [toView removeFromSuperview];
            }
            [transitionContext completeTransition:!wasCancelled];
        }
    }];
}

@end
